#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

typedef struct stu
{
	char name[20];
	int age;
}stu;
int cmp_str(const void* p1, const void* p2)
{
	return *((char*)p1) - *((char*)p2);
}
int cmp_stu_age(const void* p1, const void* p2)
{
	return ((stu*)p1)->age - ((stu*)p2)->age;
}
int cmp_int(const void* p1, const void* p2)
{
	return *((int*)p1) - *((int*)p2);
}

void Swap(char* p1, char* p2, size_t size)
{
	char t;
	for (int i = 0; i < size; ++i)
	{
		t = *p1;
		*p1 = *p2;
		*p2 = t;
		p1++;
		p2++;
	}
}



void Bubbling_sort(const void* base, size_t num, size_t size, int (*cmp)(const void*, const void*))
{
	for (int i = 0; i < num - 1; ++i)
	{
		for (int j = 0; j < num - 1 - i; ++j)
		{
			if (cmp((char*)base + j * size, (char*)base + (j + 1) * size) > 0)
			{
				Swap((char*)base + j * size, (char*)base + (j + 1) * size, size);
			}
		}
	}
}




int main()
{
	int irr[10] = { 3, 5, 9, 45, 58, 25, 47, 69, 98, 21 };
	char crr[] = { "sdakfgasg" };
	stu srr[3] = { {"zhangsan",28},{"lisi",58},{"wangwu",19} };
	Bubbling_sort(irr, sizeof(irr)/sizeof(irr[0]), sizeof(irr[0]), cmp_int);
	Bubbling_sort(crr, sizeof(crr) / sizeof(crr[0]) - 1, sizeof(crr[0]), cmp_str);
	Bubbling_sort(srr, sizeof(srr) / sizeof(srr[0]), sizeof(srr[0]), cmp_stu_age);
	printf("%s\n", crr);
	for (int i = 0; i < 3; ++i)
	{
		printf("%s %d \n", srr[i].name, srr[i].age);
	}
	for (int i = 0; i < 10; ++i)
	{
		printf("%d ", irr[i]);
	}
	
	return 0;
}